package com.shiwaixiangcun.customer.module.watch;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.GlobalConfig;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.base.BaseActivity;
import com.shiwaixiangcun.customer.entity.BindWatchEntity;
import com.shiwaixiangcun.customer.entity.ResponseEntity;
import com.shiwaixiangcun.customer.network.StringDialogCallBack;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.JsonUtil;
import com.shiwaixiangcun.customer.widget.ChangeLightImageView;

import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Administrator
 *         手动输入号码绑定
 */
public class BindWatchSelfActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.back_left)
    ChangeLightImageView mBackLeft;
    @BindView(R.id.tv_page_name)
    TextView mTvPageName;
    @BindView(R.id.edt_watch_imei)
    EditText mEdtWatchImei;
    @BindView(R.id.btn_bind_watch)
    Button mBtnBindWatch;
    Bundle bundle = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_watch_self);
        ButterKnife.bind(this);
        initViewAndEvent();
    }

    private void initViewAndEvent() {
        mBtnBindWatch.setOnClickListener(this);
        mBackLeft.setOnClickListener(this);
        mTvPageName.setText("手动绑定手表");
    }

    /**
     * 验证IMEI号
     *
     * @param result
     */
    private void validateIMEI(String result) {


        OkGo.<String>post(GlobalApi.INSTANCE.getWatch_bind())

                .params("access_token", (String) AppSharePreferenceMgr.get(mContext, GlobalConfig.TOKEN, ""))
                .params("imei", result)
                .execute(new StringDialogCallBack(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Type type = new TypeToken<ResponseEntity<BindWatchEntity>>() {
                        }.getType();
                        ResponseEntity<BindWatchEntity> responseEntity = JsonUtil.fromJson(response.body(), type);
                        if (responseEntity == null) {
                            return;
                        }
                        switch (responseEntity.getResponseCode()) {
                            case 1001:
                                bundle = new Bundle();
                                AppSharePreferenceMgr.put(mContext, GlobalConfig.IS_BIND_WATCH, true);
                                showToastShort("绑定成功");
                                bundle.putLong("deviceId", responseEntity.getData().getHardwareId());
                                readyGoThenKill(BindSuccessActivity.class, bundle);
                                break;
                            case 1002:
                                showDialog(responseEntity.getMessage());
                                break;
                            default:
                                showDialog("绑定失败，该智能手表已经被其他用户绑定");
                                break;
                        }

                    }
                });


    }

    public void showDialog(String content) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(content);

        builder.setPositiveButton("重新输入", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                readyGo(BindWatchActivity.class);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_left:
                finish();
                break;
            case R.id.btn_bind_watch:
                String imei = mEdtWatchImei.getText().toString();
                validateIMEI(imei);
                break;
            default:
                break;

        }
    }
}
